﻿//查询医院
selectAjax("SelectByResponsibleHospital", "yiyuan");
//查询科室
selectAjax("SelectByResponsibleDepartment", "keshi");
//查询级别
selectAjax("SelectByLevel", "jibie");

function selectAjax(url, attribute) {
    $.ajax({
        url: "http://localhost:8989/hospital/FollowUpGroup/" + url,
        type: "post",
        dataType: "json",
        success: function (data) {
            for (let dataKey in data) {
                if (data[dataKey].responsibleHospital != undefined) {
                    let datas = data[dataKey].responsibleHospital;
                    $("#" + attribute).after('<option value=' + datas + '>' + datas + '</option>');
                }
                if (data[dataKey].responsibleDepartment != undefined) {
                    let datas = data[dataKey].responsibleDepartment;
                    $("#" + attribute).after('<option value=' + datas + '>' + datas + '</option>');
                }
                if (data[dataKey].level != undefined) {
                    let datas = data[dataKey].level;
                    $("#" + attribute).after('<option value=' + datas + '>' + datas + '</option>');
                }
            }
        }
    });
}

//angular js
angular.module('fenZu', []).controller('fenZuView', function ($scope) {
    //点击保存按钮触发
    $scope.save = function () {
        //style样式
        let style = "style='color: #f8c815' class='glyphicon glyphicon-warning-sign'";
        let styles = "style='color: #00cc03' class='glyphicon glyphicon-warning-sign'";
        //医院
        let yiyuans = $("#yiyuans option:selected").val();
        //科室
        let keshis = $("#keshis option:selected").val();
        //级别
        let jibies = $("#jibies option:selected").val();
        //开始日期
        let riqi = $("#riqi").val();
        //结束日期
        let jieshuriqi = $("#jieshuriqi").val();
        //日期比较
        let date1 = new Date(riqi);
        let date2 = new Date(jieshuriqi);
        //联系电话
        let phone = $("#phone").val();
        //负责人
        let fuze = $("#fuze").val();
        //课题编号
        let bianhao = $("#bianhao").val();
        //名称
        let mingcheng = $("#mingcheng").val();
        //患者数量
        let huanzheshuliang = $("#huanzheshuliang").val();
        //对照组
        let duizhaozu = $("#duizhaozu").val();
        // 状态
        let zhuangtai = $("#zhuangtai option:selected").val();
        //背景
        let beijing = $("#beijing").val();
        //空串验证以及正则验证
        if (yiyuans == "请选择") {
            $scope.state = "请选择医院";
            $("#div").html("<div " + style + "></div>&nbsp;" + $scope.state);
        } else if (keshis == "请选择") {
            $scope.state = "请选择科室";
            $("#div").html("<div " + style + "></div>&nbsp;" + $scope.state);
        } else if (riqi == null || riqi == "") {
            $scope.state = "请选择开始日期";
            $("#div").html("<div " + style + "></div>&nbsp;" + $scope.state);
        } else if (jibies == "请选择") {
            $scope.state = "请选择级别";
            $("#div").html("<div " + style + "></div>&nbsp;" + $scope.state);
        } else if (jieshuriqi == null || jieshuriqi == "") {
            $scope.state = "请选择结束日期";
            $("#div").html("<div " + style + "></div>&nbsp;" + $scope.state);
        } else if (date2 < date1) {
            $scope.state = "结束日期不能小于开始日期";
            $("#div").html("<div " + style + "></div>&nbsp;" + $scope.state);
        } else if (phone == null || phone == "") {
            $scope.state = "请填写联系电话";
            $("#div").html("<div " + style + "></div>&nbsp;" + $scope.state);
        } else if (!(/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/.test(phone))) {
            $scope.state = "请正确填写联系电话";
            $("#div").html("<div " + style + "></div>&nbsp;" + $scope.state);
        } else if (fuze == null || fuze == "") {
            $scope.state = "请填写负责人";
            $("#div").html("<div " + style + "></div>&nbsp;" + $scope.state);
        } else if (!(/^[\u4e00-\u9fa5]{0,}$/.test(fuze))) {
            $scope.state = "请正确填写负责人";
            $("#div").html("<div " + style + "></div>&nbsp;" + $scope.state);
        } else if (bianhao == null || bianhao == "") {
            $scope.state = "请填写编号";
            $("#div").html("<div " + style + "></div>&nbsp;" + $scope.state);
        } else if (mingcheng == null || mingcheng == "") {
            $scope.state = "请填写名称";
            $("#div").html("<div " + style + "></div>&nbsp;" + $scope.state);
        } else if (!(/^[\u4E00-\u9FA5A-Za-z0-9]+$/.test(mingcheng))) {
            $scope.state = "请正确填写名称";
            $("#div").html("<div " + style + "></div>&nbsp;" + $scope.state);
        } else if (huanzheshuliang == null || huanzheshuliang == "") {
            $scope.state = "请填写患者数量";
            $("#div").html("<div " + style + "></div>&nbsp;" + $scope.state);
        } else if (!(/^[1-9]\d*$/.test(huanzheshuliang))) {
            $scope.state = "请正确填写患者数量";
            $("#div").html("<div " + style + "></div>&nbsp;" + $scope.state);
        } else if (duizhaozu == null || duizhaozu == "") {
            $scope.state = "请填写对照组";
            $("#div").html("<div " + style + "></div>&nbsp;" + $scope.state);
        } else if (zhuangtai == "请选择") {
            $scope.state = "请选择状态";
            $("#div").html("<div " + style + "></div>&nbsp;" + $scope.state);
        } else if (beijing == null || beijing == "") {
            $scope.state = "请填写背景";
            $("#div").html("<div " + style + "></div>&nbsp;" + $scope.state);
        } else {
            $scope.state = "";
            $("#div").html("" + $scope.state);
        }
        let datas = {
            "responsibleHospital": yiyuans,
            "responsibleDepartment": keshis,
            "level": jibies,
            "startDate": riqi,
            "endDate": jieshuriqi,
            "contactNumber": phone,
            "personInCharge": fuze,
            "projectNo": bianhao,
            "name": mingcheng,
            "numberOfPatients": huanzheshuliang,
            "controlGroup": duizhaozu,
            "state": zhuangtai,
            "background": beijing,
        }
        //判断没有错误信息的时候进行保存
        if ($scope.state == "") {
            $.ajax({
                url: "http://localhost:8989/hospital/FollowUpGroup/Insert",
                type: "post",
                data: datas,
                dataType: "json",
                success: function (status) {
                    if (status == true) {
                        $scope.state = "操作成功";
                        $("#div").html("<div " + styles + "></div>&nbsp;" + "<span style='color: #2aabd2'>" + $scope.state + "</span>");
                        setTimeout(function () {
                            location.href="分组管理.html";
                        }, 2000)
                    } else {
                        $scope.state = "操作失败";
                        $("#div").html("<div " + style + "></div>&nbsp;" + $scope.state);
                    }
                },
                error: function () {
                    $scope.state = "操作失败";
                    $("#div").html("<div " + style + "></div>&nbsp;" + $scope.state);
                }
            })
        }
    }
});